package com.szqhyxj.youxiangju.biz.partnerHome.shop;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.OrderDeetails;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;
import com.szqhyxj.youxiangju.sys.util.StringUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * XiaoPanYu
 * on 2018/11/5
 */
public class SubordinateOrdersAdapter extends BaseAdapter{


    private List<OrderDeetails> deetails;
     OnClickitem listener;
    public SubordinateOrdersAdapter(List<OrderDeetails> deetails, OnClickitem listener){
        this.deetails = deetails;
        this.listener = listener;
    }

    @Override
    public int getCount(){
        return deetails.size();
    }

    @Override
    public Object getItem(int i){
        return deetails.get(i);
    }

    @Override
    public long getItemId(int i){
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup){
        if(view == null){
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_suborinate_order_item, null);
            ViewHolder holder = new ViewHolder(view);
            view.setTag(holder);
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        OrderDeetails order = deetails.get(i);
        holder.orderId.setText(order.getPayOrderId());
        holder.price.setText(StringUtils.initPrice(order.getTotalPrice()));
        holder.addTime.setText(order.getAddTime());
        holder.addTime.setVisibility(View.GONE);
        holder.type.setText(order.getBrandSubName());
        holder.total.setText(StringUtils.initPrice(order.getTotalPrice()));
        holder.deliveryNow.setVisibility(View.GONE);
        holder.typeAwait.setVisibility(View.GONE);
        LoadImageUtils.loadImage(holder.icon,order.getFile());

        OnBtnClickListener onBtnClickListener = new OnBtnClickListener();
        onBtnClickListener.setI(i);
        holder.deliveryNow.setOnClickListener(onBtnClickListener);

        if(order.getDeliverStatus().equals("0")){
            holder.deliveryNow.setVisibility(View.VISIBLE);
        }else{
            holder.typeAwait.setVisibility(View.VISIBLE);
        }
        return view;
    }

    static class ViewHolder{
        @BindView(R.id.order_id)
        TextView orderId;
        @BindView(R.id.add_time)
        TextView addTime;
        @BindView(R.id.icon)
        ImageView icon;
        @BindView(R.id.commodity_name)
        TextView commodityName;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.quantity)
        TextView quantity;
        @BindView(R.id.delivery_now)
        Button deliveryNow;

        @BindView(R.id.type_await)
        TextView typeAwait;
        @BindView(R.id.type)
        TextView type;
        @BindView(R.id.total)
        TextView total;

        ViewHolder(View view){
            ButterKnife.bind(this, view);
        }
    }


    class OnBtnClickListener implements View.OnClickListener{
        private int i;

        public OnBtnClickListener(){
            this.i = i;
        }

        @Override
        public void onClick(View view){
            switch(view.getId()){
                case R.id.delivery_now:
                    listener.onBuyNow(i);
                    break;
            }
        }

        void setI(int i){
            this.i = i;
        }


    }

    public interface OnClickitem{
        void onBuyNow(int i);
    }
}
